// chrono @ 2021-09

export default { say_hello, header_filter, body_filter }

function say_hello(r)
{
  let str = 'hello ' +
            r.variables['hostname'] + ' ' +
            r.variables['scheme'] + '://' +
            r.variables['host'] + ' ' +
            r.variables['request_uri'] + '\n'

  if (r.headersIn['test'] == '1') {
    r.headersOut['test'] = 'xxx'
  }

  r.return(200, str)
}

let trailer = '<added>\n'

function header_filter(r)
{
    r.headersOut['njs'] = njs.version

    r.headersOut['Content-Length'] =
      parseInt(r.headersOut['Content-Length']) + trailer.length
}

function body_filter(r, data, flags)
{
   data = data.toUpperCase()

  if (flags.last) {
    data = data + trailer
  }

  r.sendBuffer(data, flags);
}

